package com.wzapp.sitposture.usercenter.repository;

import com.wzapp.sitposture.usercenter.entity.UserLoginLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.sql.Timestamp;

/**
 * Created by qianglong on 2018/3/25.
 */
@RepositoryRestResource(exported = false)
public interface UserLoginLogRepository extends JpaRepository<UserLoginLog,String>,QueryDslPredicateExecutor {

    /**
     * 功能:查询最近一次用户登录记录 <br/>
     *
     * @author ql
     * @create 2018-05-08 10:57
     * @param userId
     * @param terminalType
     * @return UserLoginLog
     **/
    UserLoginLog findTop1ByUserIdAndTerminalTypeAndLogoutTimeIsNullOrderByCreateTimeDesc(String userId,String terminalType);

    /**
     * 功能: 获取最近登录时间<br/>
     *
     * @author ql
     * @create 2018/7/25 13:09
     * @param userId
     * @return UserLoginLog
     **/
    @Query(nativeQuery = true,value = "SELECT login_time from user_login_log where user_id=?1 order by login_time " +
            "desc limit 1")
    Timestamp findLastLoginTimeByUserIdOrderByLoginTimeDesc(String userId);

    @Query(value="select user_id from user_login_log group by user_id having max(create_time)>?1 order by ?#{#pageable}",nativeQuery = true)
    Page<String> findUserIdByLoginTimeGreaterThan(Timestamp loginTime, Pageable pageable);
}
